package com.tebie.cloud.task.server.service.impl;

import com.tebie.cloud.task.server.service.ReserveSchedulerService;
import com.tebie.cloud.task.server.service.feign.ReserveClient;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.order.client.model.entity.Reserve;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ReserveSchedulerServiceImpl implements ReserveSchedulerService {

    @Autowired
    ReserveClient reserveClient;

    @Override
    public void doReserveStatusByExpiredJob(int status) {
        log.info("执行排队单关闭定时器");
        long currentTime = System.currentTimeMillis();
        //查询待服务排号单
        ResultBody<List<Reserve>> reserveResultBody = reserveClient.selectReserveList(null,null,"2");
        if(reserveResultBody!=null && reserveResultBody.getCode() == ResultEnum.OK.getCode()){
            List<Reserve> reserveLiset = reserveResultBody.getData();
            if(reserveLiset!=null && reserveLiset.size()>0){
                for(Reserve reserve:reserveLiset){
                    long limittime = currentTime-reserve.getUt().getTime();
                    if(limittime>10*60*1000){//超过10分钟，修改过号
                        reserveClient.update(reserve.getReserveId(),status);
                        //通知门店排号
                        reserveClient.noticeReserve(reserve.getStoreId());
                    }
                }
            }
        }
    }
}
